package com.jlnk.androidfmc;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.jlnk.androidfmc.entity.News;
import com.jlnk.androidfmc.fragment.ConsultFragment;
import com.jlnk.androidfmc.fragment.HotNewFragment;
import com.jlnk.androidfmc.fragment.ZnbNewFragment;
import com.jlnk.androidfmc.view.NewsPost;

public class SecondActivity extends AppCompatActivity {

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private FrameLayout frameLayout;
    private ImageButton mButton;
    private HotNewFragment hotNewFragment;
    private static final int NEWS_POST_REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_njxt);

        mButton = findViewById(R.id.image_button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, NewsPost.class);
                startActivityForResult(intent, NEWS_POST_REQUEST_CODE);
            }
        });

        fragmentManager = getSupportFragmentManager();
        frameLayout = findViewById(R.id.fragment_container);

        Button buttonHotNews = findViewById(R.id.button_hot_news);
        Button buttonExpertAdvice = findViewById(R.id.button_expert_advice);
        Button buttonSupportNews = findViewById(R.id.button_support_news);

        buttonHotNews.setOnClickListener(view -> onNewClick(view));
        buttonExpertAdvice.setOnClickListener(view -> onExpertAdviceClick(view));
        buttonSupportNews.setOnClickListener(view -> onSupportNewsClick(view));

        hotNewFragment = new HotNewFragment(); // 初始化 hotNewFragment
        loadFragment(hotNewFragment); // 加载初始 Fragment
    }

    private void loadFragment(Fragment fragment) {
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }

    public void onNewClick(View view) {
        loadFragment(new HotNewFragment());
    }

    public void onExpertAdviceClick(View view) {
        loadFragment(new ConsultFragment());
    }

    public void onSupportNewsClick(View view) {
        loadFragment(new ZnbNewFragment());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == NEWS_POST_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            // 从 Intent 中获取 new_news 对象
            News newNews = data.getParcelableExtra("new_news");
            if (hotNewFragment != null && newNews != null) {
                hotNewFragment.addNews(newNews); // 更新新闻列表
                hotNewFragment.refreshList(); // 刷新新闻列表视图
            }
        }
    }
}